const util = require('../../utils/util')
const regeneratorRuntime = require('../../utils/runtime')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cate_id: '',
    keyword: '',
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // if (getCurrentPages().length == 1 && !wx.getStorageSync('route')) {
    //   wx.setStorageSync('route', 'init')
    //   wx.reLaunch({
    //     url: `/pages/index/index?re=/pages/list/list&params=cate_id:${options.cate_id}-catename:${options.catename}`
    //   })
    // }
    wx.setStorageSync('init', 'true')
    const _this = this
    if (!wx.getStorageSync('token')) {
      // 登录
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          wx.request({
            url: app.globalData.requestUrl + '/api/home/login/?code=' + res.code,
            data: {},
            header: {
              'content-type': 'application/json' // 默认值
            },
            success(res) {
              if (res.data.code === 1) {
                wx.setStorageSync('token', res.data.data.token)
                wx.setNavigationBarTitle({
                  title: decodeURIComponent(options.catename)
                })
                _this.setData({
                  cate_id: options.cate_id
                })
                _this.getList({
                  keyword: this.data.keyword,
                  cate_id: this.data.cate_id
                })
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        }
      })
    } else {
      wx.setNavigationBarTitle({
        title: decodeURIComponent(options.catename)
      })
      this.setData({
        cate_id: options.cate_id
      })
      this.getList({
        keyword: this.data.keyword,
        cate_id: this.data.cate_id
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },

  tabDisplay (e) {
    let Index = e.currentTarget.dataset.index
    let typeId = e.currentTarget.dataset.typeid
    let cateId = this.data.cate_id
    let List = this.data.list
    List[Index]['display'] = !List[Index]['display']
    if (List[Index]['display']) {
      util.WXRequest('/api/user/log', {
        cate_id: cateId,
        type_id: typeId
      }, "GET", 'noLoading').then(res => {
        console.log(res)
      })
    }
    this.setData({
      list: List
    })
  },
  inputKeyword (e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  submit () {
    if (this.data.keyword) {
      this.getList({
        keyword: this.data.keyword,
        cate_id: this.data.cate_id
      }, 'search')
    } else {
      wx.showToast({
        title: '请输入关键词！',
        icon: 'none',
        duration: 2000
      })
    }
  },
  getList (data, type = '') {
    util.WXRequest('/api/user/getProduct', data).then(res => {
      if (res.code === 1) {
        let currentList = res.data.map((item, index) => {
          item.display = false
          return item
        })
        this.setData({
          list: currentList
        })
        if (type == 'search') {
          // 展开
          for (let i = 0; i < res.data.length; i++) {
            let e = {
              currentTarget: {
                dataset: {
                  index: i,
                  typeid: res.data[i].id
                }
              }
            }
            this.tabDisplay(e)
          }
        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  toLink (e) {
    wx.navigateTo({
      url: '/pages/infomation/infomation?id=' + e.currentTarget.dataset.id
    })
  }
})